package in.muton.edushare.panels.upload;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.pages.common.EdusharePanel;

import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.FileDirectories;

public abstract class MultipleFileUploadButtonPanel extends EdusharePanel {
	
	private static final long serialVersionUID = -7536487591058677387L;
	private Integer uploadedFilesCountInSession;
	
	public MultipleFileUploadButtonPanel(String id, final IModel<FileDirectories> directoryModel){
		super(id, directoryModel);
		final ModalWindow modalUploadWin = new ModalWindow("modalUploadWin");
		modalUploadWin.setPageCreator(new ModalWindow.PageCreator() {
			
			private static final long serialVersionUID = -4016010005571485101L;

			public Page createPage() {
				return new MultipleFileUploadPanelModalPage((IModel<FileDirectories>)getDefaultModel(), modalUploadWin);
			}
		});
		
		modalUploadWin.setCloseButtonCallback(new ModalWindow.CloseButtonCallback(){

			private static final long serialVersionUID = 6536628247742302599L;

			public boolean onCloseButtonClicked(AjaxRequestTarget target) {
				onUploadWindowClose(target);
				return true;
			}
			
		});
		
		modalUploadWin.setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {
			
			private static final long serialVersionUID = -1069870551555124704L;

			public void onClose(AjaxRequestTarget target) {
				onUploadWindowClose(target);
			}
		});
		modalUploadWin.setWidthUnit("px");
		modalUploadWin.setInitialWidth(400);
		modalUploadWin.setHeightUnit("px");
		modalUploadWin.setInitialHeight(240);
		add(modalUploadWin);
		
		AjaxButton openUploadWin = new AjaxButton("openUploadWin"){

			private static final long serialVersionUID = -2126775390275428350L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				uploadedFilesCountInSession = EduShareSession.get().getFilesUploadedThisSession();
				modalUploadWin.show(target);
			}
			
		};
		add(openUploadWin);
		
	}
	
	public abstract void onUploadWindowClose(AjaxRequestTarget target); 
	
	public int getFilesUploadedBeforeButtomClicked(){
		return this.uploadedFilesCountInSession;
	}
}
